﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using MetadataDocumentationGenerator.Properties;

namespace MetadataDocumentationGenerator
{
    [Flags]
    public enum AttributeSelectionOption
    {
        AllAttributes,
        AttributesOnForm,
        AttributesOptionSet,
        AttributeManualySelected
    }

    [Flags]
    public enum Output
    {
        Excel,
        Word,
        Csv,
        SureStepExcel,
        SureStepWord
    }

    /// <summary>
    /// Class to store list of entities
    /// </summary>
    public class EntityItem
    {
        public string Name { get; set; }
        public List<string> Attributes { get; set; } 
    }

    /// <summary>
    /// Settings for document generation
    /// </summary>
    public class GenerationSettings : ICloneable
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of class GenerationSettings
        /// </summary>
        public GenerationSettings()
        {
            EntitiesToProceed = new List<EntityItem>();
        }

        #endregion Constructor

        #region Properties

        public List<EntityItem> EntitiesToProceed { get; set; }

        public string FilePath { get; set; }

        public bool IncludeOnlyAttributesOnForms { get; set; }

        public int DisplayNamesLangugageCode { get; set; }

        public bool AddAuditInformation { get; set; }

        public bool AddFieldSecureInformation { get; set; }

        public bool AddRequiredLevelInformation { get; set; }

        public bool AddValidForAdvancedFind { get; set; }

        public bool AddEntitiesSummary { get; set; }

        public AttributeSelectionOption AttributesSelection { get; set; }

        public Output OutputDocumentType { get; set; }

        #endregion Properties

        #region Methods

        public static GenerationSettings CreateFromFile(string path)
        {
            using(var reader = new StreamReader(path))
            {
                var doc = new XmlDocument();
                doc.LoadXml(reader.ReadToEnd());

                return (GenerationSettings)XmlSerializerHelper.Deserialize(doc.OuterXml, typeof(GenerationSettings));
            }
        }

        public void SaveToFile()
        {
            var sfDialog = new SaveFileDialog
                               {
                                   Title = Resources.GenerationSettingsSaveFileDialogTitle,
                                   Filter = Resources.SettingsDialogFilter
                               };

            if(sfDialog.ShowDialog() == DialogResult.OK)
            {
                XmlSerializerHelper.SerializeToFile(this, sfDialog.FileName);
            }
        }

        public object Clone()
        {
            return new GenerationSettings
                       {
                           AddAuditInformation =  AddAuditInformation,
                           AddEntitiesSummary = AddEntitiesSummary,
                           AddFieldSecureInformation = AddFieldSecureInformation,
                           AddRequiredLevelInformation = AddRequiredLevelInformation,
                           AddValidForAdvancedFind = AddValidForAdvancedFind,
                           DisplayNamesLangugageCode = DisplayNamesLangugageCode,
                           EntitiesToProceed = EntitiesToProceed,
                           FilePath = FilePath,
                           IncludeOnlyAttributesOnForms = IncludeOnlyAttributesOnForms,
                           AttributesSelection = AttributesSelection
                       };
        }

        #endregion Methods
    }
}
